<?php

namespace App\Http\Controllers;

use App\Common\Sql;

//获取兑换活动列表
class ExchangeActionList extends Base
{

    public function logic()
    {
        if (!isset($this->param['uid'])) {
            return $this->returnError(1, "please input uid");
        }
        $uid      = $this->param['uid'];
        $data     = array();
        $cur_time = time();
        $row = Sql::select('t_item_exchange_conf',[['status',1]]);
        foreach ($row as $info) {
            $act_info   = array();
            $begin_time = strtotime($info['begin_time']);
            $end_time   = strtotime($info['end_time']);
            //检查是否在活动时间内
            if ($cur_time < $begin_time || $cur_time > $end_time) {
                continue;
            }
            $actid            = (int)$info['id'];
            $act_info['id']   = $actid;
            $act_info['name'] = $info['name'];
            $currency         = (int)$info['currency'];
            //获取一般等价物物品的信息
            $item_info = $this->get_item_info($currency);
            if ($item_info == false) {
                continue;
            } else {
                //这里配置的物品不能是商家券
                if ((int)$item_info['type'] == 4) {
                    return $this->returnError(2, "config currency type is error");
                }
                //获取用户拥有该物品的数量
                $uid_has_item_num     = $this->get_user_item_num($uid, $currency);
                $act_info['currency'] = array('id' => $currency, 'name' => $item_info['name'], 'user_has' => $uid_has_item_num);
            }
            $act_info['begin_time'] = $info['begin_time'];
            $act_info['end_time']   = $info['end_time'];

            $rule = $info['rule'];
            //解析兑换比例
            $rules = explode('|', $rule);
            foreach ($rules as $items) {
                $item_num = explode('=', $items);
                if (count($item_num) != 2) {
                    continue;
                }
                $item['itemid']          = (int)$item_num[0];
                $item['num']             = (int)$item_num[1];
                $act_info['rule_list'][] = $item;
            }
            $data['action_list'][] = $act_info;
        }
        $data['uid'] = $uid;
        return $this->returnData($data);
    }

}